package org.realogic.cfm.persistence;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;

import org.realogic.cfm.persistence.element.KByteElement;
import org.realogic.cfm.persistence.element.KSqlDateElement;
import org.realogic.cfm.persistence.element.KDoubleElement;
import org.realogic.cfm.persistence.element.KFloatElement;
import org.realogic.cfm.persistence.element.KElement;
import org.realogic.cfm.persistence.element.KIntegerElement;
import org.realogic.cfm.persistence.element.KLongElement;
import org.realogic.cfm.persistence.element.KStringElement;
import org.realogic.cfm.persistence.element.KSqlTimeElement;
import org.realogic.cfm.persistence.element.KSqlTimestampElement;

public class KSqlParameter implements KParameter, Serializable {

	private static final long serialVersionUID = -5541067780510277663L;
	private String name;

	@SuppressWarnings("rawtypes")
	private KElement value;

	@SuppressWarnings("rawtypes")
	public KSqlParameter(KElement value) {
		this.value = value;
	}

	@SuppressWarnings("rawtypes")
	public KSqlParameter(String name, KElement value) {
		this.name = name;
		this.value = value;
	}

	public KSqlParameter(byte value_) {
		value = new KByteElement(value_);
	}

	public KSqlParameter(int value_) {
		value = new KIntegerElement(value_);
	}

	public KSqlParameter(String name_, int value_) {
		this.name = name_;
		value = new KIntegerElement(value_);
	}

	public KSqlParameter(long value_) {
		value = new KLongElement(value_);
	}

	public KSqlParameter(float value_) {
		value = new KFloatElement(value_);
	}

	public KSqlParameter(double value_) {
		value = new KDoubleElement(value_);
	}

	public KSqlParameter(String value_) {
		value = new KStringElement(value_);
	}
	
	public KSqlParameter(String name_, String value_) {
		this.name = name_;
		value = new KStringElement(value_);
	}

	public KSqlParameter(java.sql.Date value_) {
		value = new KSqlDateElement(value_);
	}

	public KSqlParameter(Time value_) {
		value = new KSqlTimeElement(value_);
	}

	public KSqlParameter(Timestamp value_) {
		value = new KSqlTimestampElement(value_);
	}

	@SuppressWarnings("unchecked")
	public int fillPreparedStatement(PreparedStatement pstmt, int index) throws SQLException {
		value.set(pstmt, index, value.getValue());
		return index;
	}

	@SuppressWarnings("rawtypes")
	public KElement getValue() {
		return value;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Class<?> geClassClass() {
		return value.getClass_();
	}
	
	public Object getValueValue() {
		return value.getValue();
	}
}